package com.log;

import com.log.aspect.SysLogAspect;
import com.log.event.SysLogListener;
import com.log.pojo.OptLogProperties;
import com.log.utils.JsonUtils;
import com.log.utils.PointUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;

/**
 * @Author zhang
 * @create 2023/8/4 17:23
 */
@Slf4j
@EnableAsync
@Configuration
@AllArgsConstructor
@ConditionalOnWebApplication
@ConditionalOnProperty(prefix = OptLogProperties.PREFIX, name = "enabled", havingValue = "true", matchIfMissing = true)
public class LogAutoConfiguration {

    @Bean
    @ConditionalOnMissingBean
    public SysLogAspect sysLogAspect() {
        log.info("创建日志切面事务器");
        return new SysLogAspect();
    }

    @Bean
    @ConditionalOnMissingBean
    @ConditionalOnExpression("${log.enabled:true} && 'LOGGER'.equals('${log.type:LOGGER}')")
    public SysLogListener sysLogListener() {
        log.info("本地日志创建切面事务器");
        return new SysLogListener(log -> PointUtil.debug("0", "OPT_LOG", JsonUtils.toJson(log)));
    }
    
}
